package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.friend.FriendLoopQuery;
import cn.wolfcode.trip.base.service.IFriendService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonRawValue;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.print.DocFlavor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("friends")
public class FriendController {
    @Autowired
    private IFriendService friendService;

    @PostMapping
    public JSONResult addFriend(Friend friend){
        JSONResult result = new JSONResult();
        try {
            friendService.addFriend(friend);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @GetMapping("/travels")
    public Object getFriendLoop(FriendLoopQuery qo){
        PageInfo pageInfo = friendService.getFriendLoop(qo);
        return pageInfo;
    }

    @GetMapping("/attentionTravels")
    public Object getAttentionFriendLoop(FriendLoopQuery qo){
        PageInfo pageInfo = friendService.getAttentionFriendLoop(qo);
        return pageInfo;
    }

    @GetMapping("/groups/{id}")
    public Object getGroupList(@PathVariable Long id){
        List<Group> list = friendService.getGroupByUserId(id);
        return list;
    }

    @GetMapping("/groups/friends")
    public Object getFriendList(Long groupId){
        List<Friend> list = friendService.getFriendByGroupId(groupId);
        return list;
    }

    @PostMapping("/group")
    public JSONResult addGroup(Group group){
        JSONResult result = new JSONResult();
        try {
            friendService.addGroup(group);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @PostMapping("/moveGroup")
    public JSONResult moveGroup(Long groupId,Long friendId,Long userId){
        JSONResult result = new JSONResult();
        try {
            friendService.moveGroup(groupId,friendId,userId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


    @DeleteMapping
    public JSONResult deleteFriend(Long userId,Long friendId){
        JSONResult result = new JSONResult();
        try {
            friendService.deleteFriend(userId,friendId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


    @PostMapping("/crowds")
    public JSONResult addCrowd(Crowd crowd){
        JSONResult result = new JSONResult();
        try {
            friendService.addCrowd(crowd);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @GetMapping("/crowds")
    public Object getCrowds(Long id){
        List<Crowd> list = friendService.getListForCrowds(id);
        return list;
    }

    @GetMapping("/crowds/users/{id}")
    public Object getFriendByCrowd(@PathVariable Long id){
        List<User> list = friendService.getFriendByCrowd(id);
        return list;
    }

    @GetMapping("/crowds/{id}")
    public Object getCrowdById(@PathVariable Long id){
        Crowd crowd = friendService.getCrowdById(id);
        return crowd;
    }

    @PostMapping("/crowdRelation")
    public JSONResult saveCrowdRelation(Long crowdId,Long userId){
        JSONResult result = new JSONResult();
        try {
            friendService.saveCrowdRelation(crowdId,userId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @DeleteMapping("/crowdRelation")
    public JSONResult deleteCrowdRelation(Long crowdId,Long userId){
        JSONResult result = new JSONResult();
        try {
            friendService.deleteCrowdRelation(crowdId,userId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
